package com.sztx.pay.center.rpc.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance.UserActType;
import com.sztx.pay.center.common.util.JFUtils;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.AccountService;
import com.sztx.pay.center.core.service.ActService;
import com.sztx.pay.center.core.service.UserInfoService;
import com.sztx.pay.center.dataaccess.dao.mysql.UserActMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.UserActDO;
import com.sztx.pay.center.rpc.api.service.QueryWithDrawLimitService;
import com.sztx.pay.risk.rpc.api.PayRiskService;
import com.sztx.pay.risk.rpc.api.domain.TradeType;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.core.service.impl.BaseServiceImpl;
import com.sztx.se.rpc.dubbo.client.DubboClientFactory;
@Service
public class QueryWithDrawLimitServiceImpl extends BaseServiceImpl implements QueryWithDrawLimitService{
	@Autowired
	UserActMysqlDAO userActMysqlDAO;
	
	PayRiskService payRiskService;
	@Autowired
	AccountService accountService;
	@Autowired
	UserInfoService userInfoService;
	@Autowired
	ActService actService;
	@Autowired(required=false)
	private DubboClientFactory dubboConsumerFactory;
	@Override
	@SlaveQueryAnnotation
	public long queryLimit(String userId,String channelId) {
		
		boolean isJFchannel = JFUtils.isJFChannel(channelId);
		UserActType userActType = null;
		if(isJFchannel){
			userActType = UserActType.JF;
		}else{
			userActType = UserActType.CASH;
		}
		
		
		UserActDO userActDO = actService.findValidActInfo(userId, userActType, false);
		if(userActDO == null){
			throw new BusinessException("未查询到账户【"+userId+"】");
		}
		
		
		//查询账户是否已存在 		
		Integer userLevel = userActDO.getActLevel();
		Integer userType = userActDO.getUserType();
		//Integer actType = userActDO.getActType();
		
		
		
		
		TradeType tradeType = TradeType.WITHDRAW_BERBON_BANKCARD;
		payRiskService = dubboConsumerFactory.getDubboClient("payRiskService");
		int limit = payRiskService.getUserWithdrawLimit(userId, userType, userLevel, channelId, tradeType);
//		int banlance = accountService.queryBalance(userId, actType, false);
//		if(limit == -1){
//			limit = banlance;
//		}else if(limit > 0){
//			limit = (limit >= banlance) ? banlance : limit;
//		}
		return limit;
	}

}
